//
//  AudioManager.m
//  DoctorYL
//
//  Created by 张玺 on 15/2/10.
//  Copyright (c) 2015年 yuntai. All rights reserved.
//

#import "AudioManager.h"
#import "Base64.h"
#import "TFAudioPlayer.h"
@implementation AudioManager

+ (NSString *)audioPathWithString:(NSString *)audioString {
    
    NSData *data = [audioString base64DecodedData];
    
    // 获取沙盒目录
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp3",[NSUUID UUID].UUIDString]];
    
    // 将录音写入文件
    [data writeToFile:fullPath atomically:YES];
    WXLog(@"fullPath:%@",fullPath);
    
    NSString *mp3Path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"myRecord.mp3"];
    if ([[NSFileManager defaultManager] removeItemAtPath:mp3Path error:nil]) {
        WXLog(@"原MP3录音删除成功");
    }
    
    return fullPath;
}

+(CGFloat)audioDurationWithPath:(NSString *)path
{
    NSError *error;
    TFAudioPlayer *player = [[TFAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:path] error:&error];
    if (error) {
        WXLog(@"录音时长有问题:%@",error);
    }
    return player.duration;
}
@end
